<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>市场活动</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <!--<script src="../js/activity.js" type="text/javascript" charset="utf-8"></script>-->
    <link rel="stylesheet" type="text/css" href="../layui/css/layui.css" media="all" />
    <script src="../layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <!--<link rel="stylesheet" type="text/css" href="../css/customer.css" />-->
    <style type="text/css">
        .layui-table th,
        .layui-table td {
            text-align: center;
        }

        .layui-form {
            margin-top: 20px;
        }
    .layui-form-item{
        padding-left: 50px;
    }
        .ac_button {
            float: right;
            margin-right: 80px;
        }
        .search_btn{
            margin-left: -10px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" layui-filter="cu_form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">场景</label>
            <div class="layui-input-inline">
                <select name="modules" lay-verify="required" lay-search="" lay-filter="ac_select">
                    <option value="1">所有活动</option>
                    <option value="2">已结束活动</option>
                </select>
            </div>
        </div>

        <div class="layui-inline" style="position: relative;">
            <div class="ac_search">
                <div class="layui-input-inline" id="search_box">
                    <i class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius layui-icon layui-icon-close layui-hide" id="inputClear" style="position: absolute;float: right;right: 10px;top: 8px;"></i>
                    <input class="layui-input" type="text" name="id" id="ac_input" autocomplete="off" placeholder="请输入活动名称"/>
                </div>
                <div class="layui-btn search_btn" data-type="reload" id="search_btn" style="float: right;">搜索 <i class="layui-icon">&#xe615;</i></div>
            </div>
        </div>

        <div class="layui-btn-group ac_button" id="ac_button">
            <button type="button" class="layui-btn" data-method="offset" data-type="auto" id="ac_create">新建活动<i class="layui-icon">&#xe654;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" id="ac_del">删除活动<i class="layui-icon">&#xe640;</i></button>
        </div>
    </div>
</form>

<!-- 这里是弹出层的表单信息 -->
<div class="layui-login-background" id="add_activity" style="display:none;">
    <div class="layui-form layui-form">
        <form class="layui-form" action="" id="popupForm" lay-filter="popupFormFilter">
            <div class="layui-form-item logo-title">
                <h1>新建活动</h1>
            </div>
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>基本信息</legend>
            </fieldset>
            <div class="layui-form-item">
                <div class="layui-inline" style="display: none;">
                    <label class="layui-form-label" >ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" lay-verify="required" autocomplete="off" class="layui-input" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" for="ac_name">活动名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="ac_name" lay-verify="required" autocomplete="off"
                               class="layui-input"/>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" for="ac_object">关联对象</label>
                    <div class="layui-input-inline">
                        <select name="ac_object" lay-verify="required">
                            <option value=""></option>
                            <option value="线索">线索</option>
                            <option value="客户">客户</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" for="ac_type">活动类型</label>
                        <div class="layui-input-inline">
                            <select name="ac_type" lay-verify="required">
                                <option value=""></option>
                                <option value="线索">广告</option>
                                <option value="研讨会/会议">研讨会/会议</option>
                                <option value="营销">营销</option>
                                <option value="公共关系">公共关系</option>
                                <option value="合作伙伴">合作伙伴</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" >
                        <label class="layui-form-label"><font style="color: red;">*</font>开始时间</label>
                        <div class="layui-input-inline">
                            <input type="date" name="ac_start_time" class="layui-input" id="ac_start_time" placeholder="选择日期">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline" >
                        <label class="layui-form-label"><font style="color: red;">*</font>结束时间</label>
                        <div class="layui-input-inline">
                            <input type="date" name="ac_end_time" class="layui-input" id="ac_end_time" placeholder="选择日期">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" for="ac_budget">活动预算</label>
                        <div class="layui-input-inline">
                            <input type="text" name="ac_budget" lay-verify="required" autocomplete="off"
                                   class="layui-input"/>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" for="ac_site">活动地址</label>
                        <div class="layui-input-inline">
                            <input type="text" name="ac_site" lay-verify="required" autocomplete="off"
                                   class="layui-input"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">活动简介</label>
                        <div class="layui-input-inline">
                            <textarea  name="ac_introduction" placeholder="请输入内容" class="layui-textarea"
                                      id="ac_introduction"></textarea>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item add_btn">

                    <button class="layui-btn" lay-submit="save" lay-filter="save">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary" lay-filter="cancel" id="colseBtn">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>


<script src="../layui/layui.js" charset="utf-8"></script>
<table class="layui-hide" id="tb" lay-filter="ac_demo" style="margin-top: 50px;"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit"  id="ac_edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/javascript">
    $(function() {
        layui.use(["laypage", "table", "form","layer","laydate"], function () {
            var laypage = layui.laypage;
            var page, limit;
            var $ = layui.jquery;
            var form = layui.form;
            var table = layui.table;
            var element = layui.element;
            var layer=layui.layer;
            var laydate=layui.laydate;
            var popForm=layui.form;
            //1.表格渲染
            table.render({
                elem: '#tb',
                url: '../activity/findAll',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                height: 750,
                title: '活动数据表',
                cols: [
                    [
                        {
                            type: 'checkbox',
                            fixed: 'left'
                        }, {
                        field: 'id',
                        title: 'ID',
                        width: 100,
                        sort: true,
                        fixed: true
                    }, {
                        field: 'ac_name',
                        title: '活动名称',
                        width: 100
                    }, {
                        field: 'ac_object',
                        title: '关联对象',
                        width: 200
                    }, {
                        field: 'ac_type',
                        title: '活动类型',
                        width: 200
                    }, {
                        field: 'ac_start_time',
                        title: '开始时间',
                        width: 200
                    }, {
                        field: 'ac_end_time',
                        title: '截止时间',
                        width: 100,
                    }, {
                        field: 'ac_budget',
                        title: '预算',
                        width: 100,
                    }, {
                        field: 'ac_site',
                        title: '地址',
                        width: 100,
                        sort: true
                    }, {
                        field: 'ac_introduction',
                        title: '简介',
                        width: 200
                    }
                        , {
                        field: 'ac_creator',
                        title: '创建人',
                        width: 200
                    } , {
                        field: 'ac_create_time',
                        title: '创建时间时间',
                        width: 150,
                        sort: true
                    },{
                        fixed: 'right',
                        title: '操作',
                        width: 200,
                        align: 'center',
                        toolbar: '#barDemo'
                    }
                    ]
                ],
                page: true,
                id: 'testReload',
                parseData: function (res) {
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.count,
                        "data": res.data
                    }
                }

            });


            //3.监听表格操作
            table.on('tool(ac_demo)', function (obj) {
                var data = obj.data;
                var layEvent = obj.event;
                var tr = obj.tr;

                if (layEvent === 'del') { //删除
                    var checkStatus = table.checkStatus('testReload');
                    var data1 = checkStatus.data;
                    console.log("data1=" + data1)
                    var ids = [];
                    data1.forEach(function(n, i) {
                        ids.push(n.id);
                    });

                    if (ids != '') {
                        layer.confirm('真的删除' + data.ac_name + '这个活动吗?', function(index) {
                            console.log("ids===="+JSON.stringify(ids)+"=====")
                            $.ajax({
                                url: '../activity/deleteActivity',
                                type: 'POST',
                                contentType : "application/json" ,
                                dataType: 'JSON',
                                data:JSON.stringify(ids),
                                success: function(data) {
                                    if (data.code === 0) {
                                        layer.msg('删除成功');
                                        table.reload('testReload', {});
                                    } else {
                                        layer.msg('删除失败');
                                    }
                                },
                                'error': function() {
                                    layer.msg('系统错误');
                                }
                            })
                        })
                    } else {
                        layer.msg('请选择需要删除的行',{skin:'msgSkin'});
                    }


                } else if (layEvent === 'edit') { //编辑
                    console.log(data);
                    layer.open({
                        type: 1,
                        title: "修改员工信息",
                        shade: 0.8,
                        anim: 1,
                        skin: 'layui-layer-molv',
                        area: ['800px', '80%'],
                        shadeClose: false,
                        content: $("#add_activity"),
                        success: function (index) {
                            console.log(data);
                            form.val("popupFormFilter", data);

                        }
                    });


        //监听submit提交按钮 button
        form.on('submit(save)', function(data) {
            console.log(JSON.stringify(data.field));
            console.log($('#popupForm').serialize());
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "../activity/updateActivity",
                data:$('#popupForm').serialize(),
                success: function(result) {
                    if (result.code == 0) {

                        layer.msg('修改成功，1秒后自动关闭该窗口');
                        table.reload('testReload', {});
                        setTimeout(function(){

                            layer.closeAll();

                        }, 1 * 1000);
                    };
                },
                error: function() {
                    layer.msg('后台异常！未修改成功');
                }
            });
            return false;
            });
         }
    });



            //4.下拉选择框的监听
            form.on('select(ac_select)', function (data) {
                console.log(data.value); //得到被选中的值
                var value = data.value;
                if (value == 1) {
                    table.reload('testReload', {
                        url: '../activity/findAll',
                        page: {
                            curr: 1
                        }
                    });
                } else {
                    table.reload('testReload', {
                        url: '../activity/findByUserId',
                        where: {
                            cu_userid: 2

                        },
                        page: {
                            curr: 1
                        }
                    });
                }
            });

    //取消并关闭页面
    $("#colseBtn").click(function(data) {
        layer.closeAll();
        return false;
    });


    laydate.render({
        elem: '#cu_time',
        type: 'datetime'
    });


    //5.创建活动弹出层
            //alert
            $("#ac_create").click(function () {
                //input here
                layer.open({
                    type: 2,
                    shadeClose: true,
                    shade: 0.8,
                    area: ['800px', '70%'],
                    shadeClose: false,
                    content: 'addActivity.html'

                });
            });


    //6.删除所选活动
    $("#ac_del").click(function() {
        var checkStatus = table.checkStatus('testReload');
        var data = checkStatus.data;
        console.log("data=" + data)
        var ids = [];
        data.forEach(function(n, i) {
            ids.push(n.id);
        });
        console.log("ids="+ids)
        if (ids != '') {
            layer.confirm('确定删除所选项吗？', function(index) {
                console.log("ids===="+JSON.stringify(ids)+"=====")
                $.ajax({
                    url: "../activity/deleteActivity",
                    type: 'POST',
                    contentType : "application/json" ,
                    dataType: 'JSON',
                    data:JSON.stringify(ids),
                    success: function(data) {
                        if (data.code === 0) {
                            layer.msg('删除成功');
                            table.reload('testReload', {});
                        } else {
                            layer.msg('删除失败');
                        }
                    },
                    'error': function() {
                        layer.msg('系统错误');
                    }
                })
            })
        } else {
            layer.msg('请选择需要删除的行',{skin:'msgSkin'});

        }
    });
            //7.搜索按钮
            $('#search_btn').click(function () {
                var inputVal = $('#ac_input').val();
                console.log(inputVal);
                if (inputVal == '' || inputVal == null) {
                    table.reload('testReload', {
                        url: '../activity/findAll',
                        where: {
                            ac_name: inputVal

                        },
                        page: {
                            curr: 1
                        }
                    });
                } else {
                    table.reload('testReload', {
                        url: '../activity/findByName',
                        where: {
                            ac_name: inputVal

                        },
                        page: {
                            curr: 1
                        }
                    });
                }

            });


            //键盘enter键
            $('.layui-input').keydown(function (e) {
                if (e.which == 13) {
                    $("#search_btn").click();
                }
            });

            //8.清空输入框
            //显示清空按钮
            $('#search_box').mouseover(function () {
                var inputVal = $("#ac_input").val();
                $('#inputClear').removeClass('layui-hide');
            })

            //不显示清空按钮
            $('#search_box').mouseout(function () {
                var inputVal = $("#ac_input").val();
                $('#inputClear').addClass('layui-hide');
            })

            $('#inputClear').click(function () {
                var inputVal = $('#ac_input').val();
                if (inputVal != '' || inputVal != null) {
                    console.log("清空输入框")
                    $('#ac_input').val('');
                }
            })
        })


    })

</script>
</body>
</body>
</html>